package com.example.goodrubish.Garbage;

import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.goodrubish.R;

// QuestionFragment.java
public class QuestionFragment extends Fragment {
    private Question question;
    private RadioButton answerA;
    private RadioButton answerB;
    private RadioButton answerC;
    private RadioButton answerD;
    private TextView explanationText;
    private Button nextButton;
    private Button previousButton;
    private Button submitButton;

    private ViewPager viewPager;
    public static QuestionFragment newInstance(Question question) {
        QuestionFragment fragment = new QuestionFragment();
        Bundle args = new Bundle();
        args.putParcelable("question", question);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_question, container, false);
        question = getArguments().getParcelable("question");


        TextView questionText = view.findViewById(R.id.question_text);
        questionText.setText(question.question);
        ///选项
        RadioGroup radioGroup = view.findViewById(R.id.radio_group);
        answerA = view.findViewById(R.id.answer_a);
        answerB = view.findViewById(R.id.answer_b);
        answerC = view.findViewById(R.id.answer_c);
        answerD = view.findViewById(R.id.answer_d);

        answerA.setText(question.answerA);
        answerB.setText(question.answerB);
        answerC.setText(question.answerC);
        answerD.setText(question.answerD);

        explanationText = view.findViewById(R.id.explanation);
//        nextButton = view.findViewById(R.id.next_button);
//        previousButton = view.findViewById(R.id.previous_button);
        submitButton = view.findViewById(R.id.submit_button);
        submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              showSubmitDialog();
            }
        });

        radioGroup.setOnCheckedChangeListener(null); // Disable radio group listener

        answerA.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                checkAnswer(0);
            }
        });
        answerB.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                checkAnswer(1);
            }
        });
        answerC.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                checkAnswer(2);
            }
        });
        answerD.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                checkAnswer(3);
            }
        });
        // 确保视图加载后调用
//        setupButtons(viewPager,0);
        return view;
    }

//    public void setupButtons(ViewPager viewPager,int position) {
//        this.viewPager = viewPager;
//
//        if (previousButton != null) {
//            previousButton.setVisibility(position == 0 ? View.GONE : View.VISIBLE);
//        }
//        if (nextButton != null) {
//            nextButton.setVisibility(position >=0&&position<5 ? View.VISIBLE : View.GONE);
//        }
//       if (submitButton != null) {
//           submitButton.setVisibility(position == 4 ? View.VISIBLE : View.GONE);
//       }
//
//        if(viewPager!=null) {
//            previousButton.setOnClickListener(v -> {
//                if (position > 0) {
//                    viewPager.setCurrentItem(position - 1);
//                }
//            });
//
//            nextButton.setOnClickListener(v -> {
//                if (position < 5) {
//                    viewPager.setCurrentItem(position + 1);
//                }
//            });
//
//            submitButton.setOnClickListener(v -> {
//                showSubmitDialog();
//            });
//        }
//    }

    private void checkAnswer(int answerIndex) {
        if (answerIndex == question.getAnswer()) {
            // Correct answer
            setAnswerBackground(answerIndex, Color.rgb(50,116,13));
        } else {
            // Wrong answer

            setAnswerBackground(question.getAnswer(), Color.rgb(50,116,13));
            setAnswerBackground(answerIndex, Color.rgb(225,39,39));

            explanationText.setText(question.explaination);
            explanationText.setVisibility(View.VISIBLE);
        }
        // Disable other answers
        disableOtherAnswers(answerIndex);
    }

    private void setAnswerBackground(int answerIndex, int color) {
        switch (answerIndex) {
            case 0:
                answerA.setBackgroundColor(color);
                answerA.setTextColor(Color.WHITE);
                break;
            case 1:
                answerB.setBackgroundColor(color);
                answerB.setTextColor(Color.WHITE);
                break;
            case 2:
                answerC.setBackgroundColor(color);
                answerC.setTextColor(Color.WHITE);
                break;
            case 3:
                answerD.setBackgroundColor(color);
                answerD.setTextColor(Color.WHITE);
                break;
        }
    }

    private void disableOtherAnswers(int selectedAnswerIndex) {
        answerA.setEnabled(selectedAnswerIndex == 0);
        answerB.setEnabled(selectedAnswerIndex == 1);
        answerC.setEnabled(selectedAnswerIndex == 2);
        answerD.setEnabled(selectedAnswerIndex == 3);
    }


    private void showSubmitDialog() {
        new AlertDialog.Builder(getContext())
                .setTitle("提醒")
                .setMessage("你确定要提交吗？")
                .setPositiveButton("确定", (dialog, which) -> {
                    // 提交答案并跳转到其他页面
                    Intent intent = new Intent(getContext(), SortActivity.class);
                    startActivity(intent);
                })
                .setNegativeButton("取消", null)
                .show();
    }


}